<template>
  <div class="personal-archives-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>个人所有档案查看</span>
      </div>
      <div class="search-bar">
        <el-input placeholder="请输入标题、类型或内容关键词" v-model="searchQuery" class="input-with-select" @keyup.enter.native="fetchArchives">
          <el-button slot="append" icon="el-icon-search" @click="fetchArchives"></el-button>
        </el-input>
      </div>
      <el-table :data="archives" style="width: 100%" v-loading="loading">
        <el-table-column prop="archiveId" label="ID" width="80"></el-table-column>
        <el-table-column prop="title" label="档案标题"></el-table-column>
        <el-table-column prop="archiveType" label="档案类型" width="120"></el-table-column>
        <el-table-column prop="securityLevel" label="保密等级" width="120"></el-table-column>
        <el-table-column prop="receiptTime" label="接收时间" width="180"></el-table-column>
        <el-table-column prop="status" label="状态" width="100"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="viewArchiveDetails(scope.row)">查看详情</el-button>
            <el-button size="mini" type="primary" @click="downloadArchive(scope.row)" 
                       :disabled="!scope.row || !scope.row.filePath">
              <i class="el-icon-download"></i>
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalArchives">
      </el-pagination>
    </el-card>

    <!-- 档案详情对话框 -->
    <el-dialog title="档案详情" :visible.sync="detailDialogVisible" width="60%">
      <div v-if="currentArchive">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="档案ID">{{ currentArchive.archiveId }}</el-descriptions-item>
          <el-descriptions-item label="档案标题">{{ currentArchive.title }}</el-descriptions-item>
          <el-descriptions-item label="档案类型">{{ currentArchive.archiveType }}</el-descriptions-item>
          <el-descriptions-item label="保密等级">{{ currentArchive.securityLevel }}</el-descriptions-item>
          <el-descriptions-item label="物理位置">{{ currentArchive.physicalLocation || '无' }}</el-descriptions-item>
          <el-descriptions-item label="可见范围">{{ currentArchive.visibleScope }}</el-descriptions-item>
          <el-descriptions-item label="接收时间">{{ currentArchive.receiptTime }}</el-descriptions-item>
          <el-descriptions-item label="状态">{{ currentArchive.status }}</el-descriptions-item>
          <el-descriptions-item label="自动分类">{{ currentArchive.autoCategory || '无' }}</el-descriptions-item>
          <el-descriptions-item label="自动标签">{{ currentArchive.autoTags || '无' }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ currentArchive.createdAt }}</el-descriptions-item>
          <el-descriptions-item label="更新时间">{{ currentArchive.updatedAt }}</el-descriptions-item>
        </el-descriptions>
        
        <!-- 文档内容展示 -->
        <div class="ocr-content-section" v-if="currentArchive.ocrContent">
          <h4>文档内容</h4>
          <el-input
            type="textarea"
            :rows="8"
            v-model="currentArchive.ocrContent"
            :readonly="true"
            placeholder="无文档内容">
          </el-input>
        </div>
        <div class="ocr-content-section" v-else>
          <h4>文档内容</h4>
          <el-alert
            title="该档案没有文档内容"
            type="info"
            :closable="false"
            show-icon>
          </el-alert>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detailDialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="downloadArchive(currentArchive)" 
                   :disabled="!currentArchive || !currentArchive.filePath">
          <i class="el-icon-download"></i>
          下载文件
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import service from '@/utils/request';
import { downloadArchive } from '@/utils/download';

export default {
  name: 'PersonalArchives',
  data() {
    return {
      archives: [],
      currentPage: 1,
      pageSize: 10,
      totalArchives: 0,
      searchQuery: '',
      detailDialogVisible: false,
      currentArchive: null,
      loading: false,
      userId: null
    };
  },
  created() {
    // 获取当前用户ID
    const user = JSON.parse(localStorage.getItem('user'));
    if (user && user.userId) {
      this.userId = user.userId;
    }
    this.fetchArchives();
  },
  methods: {
    fetchArchives() {
      this.loading = true;
      service.get('/archive/personal-list', {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.searchQuery,
          userId: this.userId
        }
      })
      .then(res => {
        if (res.data.code === 200) {
          this.archives = res.data.data.records;
          this.totalArchives = res.data.data.total;
        } else {
          this.$message.error(res.data.message || '获取个人档案列表失败');
        }
      })
      .catch(error => {
        console.error("Error fetching personal archives:", error);
        this.$message.error('获取个人档案列表失败');
      })
      .finally(() => {
        this.loading = false;
      });
    },
    viewArchiveDetails(archive) {
      this.currentArchive = { ...archive };
      this.detailDialogVisible = true;
    },
    downloadArchive(archive) {
      downloadArchive(
        archive,
        (msg) => this.$message.success(msg),
        (msg) => this.$message.error(msg),
        (msg) => this.$message.warning(msg)
      );
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchArchives();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchArchives();
    }
  }
}
</script>

<style scoped>
.personal-archives-container {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
  width: 50%;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both
}
.box-card {
  width: 100%;
}
.ocr-content-section {
  margin-top: 20px;
}
.ocr-content-section h4 {
  margin-bottom: 10px;
  color: #606266;
}
</style> 